﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LiveWebServiceBase;
using LiveWebServiceBase.ServiceUserSettings;

namespace LiveWebServiceAdmin.ActionPages
{
    public partial class managedomain1 : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            string manageuser = string.Empty;
            string manangeorg = string.Empty;
            string manangesite = string.Empty;
            string manageaccount = string.Empty;
            string editdomain = string.Empty;

            if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]))
                manageuser = Page.Request.QueryString["manageuser"];
            else if (Settings.UserInfo.IsServiceUser(this.User.Identity.Name))
                manageuser = this.User.Identity.Name;

            if (!string.IsNullOrEmpty(Page.Request.QueryString["manageorganization"]))
                manangeorg = Page.Request.QueryString["manageorganization"];

            if (!string.IsNullOrEmpty(Page.Request.QueryString["managesite"]))
                manangesite = Page.Request.QueryString["managesite"];

            if (!string.IsNullOrEmpty(Page.Request.QueryString["manageaccount"]))
                manageaccount = Page.Request.QueryString["manageaccount"];

            if (!string.IsNullOrEmpty(Page.Request.QueryString["editeddomain"]))
                editdomain = Page.Request.QueryString["editeddomain"];


            if (!string.IsNullOrEmpty(manageuser) && !string.IsNullOrEmpty(manangeorg) &&
                !string.IsNullOrEmpty(manangesite) && !string.IsNullOrEmpty(manageaccount)
                && !string.IsNullOrEmpty(editdomain))
            {
                btnCreateNewDomain.Text = "Save";
                tbNewDomainName.Text = editdomain;
            }
        }

        protected void btnCreateNewDomain_Click(object sender, EventArgs e)
        {
            try
            {
                string manageuser = string.Empty;
                string manangeorg = string.Empty;
                string manangesite = string.Empty;
                string manageaccount = string.Empty;
                string editdomain = string.Empty;

                if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]))
                    manageuser = Page.Request.QueryString["manageuser"];
                else if (Settings.UserInfo.IsServiceUser(this.User.Identity.Name))
                    manageuser = this.User.Identity.Name;

                if (!string.IsNullOrEmpty(Page.Request.QueryString["manageorganization"]))
                    manangeorg = Page.Request.QueryString["manageorganization"];

                if (!string.IsNullOrEmpty(Page.Request.QueryString["managesite"]))
                    manangesite = Page.Request.QueryString["managesite"];

                if (!string.IsNullOrEmpty(Page.Request.QueryString["manageaccount"]))
                    manageaccount = Page.Request.QueryString["manageaccount"];

                                    if (!string.IsNullOrEmpty(Page.Request.QueryString["editeddomain"]))
                        editdomain = Page.Request.QueryString["editeddomain"];

                if (!string.IsNullOrEmpty(manageuser) && !string.IsNullOrEmpty(manangeorg) &&
                    !string.IsNullOrEmpty(manangesite) && !string.IsNullOrEmpty(manageaccount))
                {
                    UserSettings usItem = new UserSettings(manageuser);

                    if (!string.IsNullOrEmpty(editdomain))
                        usItem.EditDomain(manangeorg, manangesite, manageaccount, editdomain, tbNewDomainName.Text);
                    else
                        usItem.CreateDomain(manangeorg, manangesite, manageaccount, tbNewDomainName.Text);

                    /*Session["IsProcessed"] = null;*/
                    IsCompleted.Value = "true";
                }
            }
            catch (Exception ex)
            {
                lblErrorTextNewDomain.Text = ex.Message;
            }
        }
    }
}
